#include <QCoreApplication>
#include <QDebug>
#include "dbmanager.h"
#include <global.h>
#include <netmanager.h>

void testInsertAndRetrieveChatHistory() {
    // 创建 DbManager 实例
    DbManager dbManager;

    // 测试数据
    int senderId = 1;
    int receiverId = 2;
    QString message = "Hello, how are you?";
    MessageType messageType = PURE_STRING;

    // 插入聊天记录
    dbManager.insertChatHistory(senderId, receiverId, messageType, message);

    // 获取聊天记录
    QList<ChatHistory> chatHistory = dbManager.getChatHistory(senderId, receiverId);

    // 打印聊天记录
    for (const ChatHistory &chat : chatHistory) {
        qDebug() << "Sender ID:" << chat.user_one_id
                 << "Receiver ID:" << chat.user_two_id
                 << "Message:" << chat.message
                 << "Time:" << chat.message_time.toString()
                 << "Message Type:" << (chat.message_type == MessageType::PURE_STRING ? "String" : "not string");
    }
}

void testDbManager() {
    DbManager dbManager;

    // 插入用户
    User user1{1, "192.168.1.1", "Alice", "password123", ONLINE};
    User user2{2, "192.168.1.2", "Bob", "password456", NOT_ONLINE};

    dbManager.insertUser(user1);
    dbManager.insertUser(user2);

    // 插入好友关系
    dbManager.insertFriendRelation(1, 2);

    // 获取好友列表
    QList<Friend> friends = dbManager.getFriendList(1);

    // 打印好友列表
    for (const Friend &f : friends) {
        qDebug() << "User ID:" << f.id
                 << "Friend ID:" << f.friend_id
                 << "Friend Status:" << (f.friend_status == ONLINE ? "ONLINE" : "NOTONLINE");
    }
}

void testGetChatHistory(int userOneId, int userTwoId) {
    DbManager dbManager;

    // 测试数据
    int senderId = 1;
    int receiverId = 2;
    QString message = "Hello, how are you?";
    MessageType messageType = PURE_STRING;

    // 插入聊天记录
    dbManager.insertChatHistory(senderId, receiverId, messageType, message);

    dbManager.insertChatHistory(receiverId, senderId, messageType, message);
    // 获取聊天记录
    QList<ChatHistory> chatHistoryList = dbManager.getChatHistory(userOneId, userTwoId);

    // 打印聊天记录
    qDebug() << "Chat history between user" << userOneId << "and user" << userTwoId << ":";
    for (const ChatHistory &history : chatHistoryList) {
        qDebug() << "User 1 ID:" << history.user_one_id
                 << "User 2 ID:" << history.user_two_id
                 << "Message:" << history.message
                 << "Message Time:" << history.message_time.toString(Qt::ISODate)
                 << "Message Type:" << static_cast<int>(history.message_type);
    }
}

void insertTestInformation()
{
    DbManager dbManager;

    // 插入用户
    User user1{1, "192.168.1.1", "Alice", "password123", ONLINE};
    User user2{2, "192.168.1.2", "Bob", "password456", NOT_ONLINE};

    dbManager.insertUser(user1);
    dbManager.insertUser(user2);

    // 插入好友关系
    dbManager.insertFriendRelation(1, 2);

    // 测试数据
    int senderId = 1;
    int receiverId = 2;
    QString message = "Hello, how are you?";
    MessageType messageType = PURE_STRING;

    // 插入聊天记录
    dbManager.insertChatHistory(senderId, receiverId, messageType, message);
    dbManager.insertChatHistory(receiverId, senderId, messageType, message);
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // insertTestInformation();

    NetManager netManager;
    netManager.startServer(8888);

    // DbManager manager;
    // QList<Friend> data = manager.getFriendList(743341);
    // QList<Friend> data = manager.getAllFriend();
    // for(const Friend &f : data)
    // {
    //     qDebug() << " id :" << f.id << "friendId : " << f.friend_id;
    // }

    // manager.printAllUser();

    return a.exec();
}
